package com.ruoyi.project.module.whd.duizhang;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.YqgBarBills;
import com.ruoyi.project.domain.YqgConsumptionDetails;
import com.ruoyi.project.domain.YqgHands;
import com.ruoyi.project.service.IYqgBarBillsService;
import com.ruoyi.project.service.IYqgConsumptionDetailsService;
import com.ruoyi.project.service.IYqgHandsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 消费详细Controller
 * 
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/whd/duizhang")
public class WhdDuizhangController extends BaseController
{
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgHandsService yqgHandsService;
    /**
     * 查询消费详细列表
     */
   /* @RequiresPermissions("project:whd:duizhang:list")*/
    @GetMapping("/list")
    public TableDataInfo list(YqgConsumptionDetails yqgConsumptionDetails)
    {

        YqgBarBills BarBills=yqgBarBillsService.selectYqgBarBillsByID(yqgConsumptionDetails.getBarBillsId());
        System.out.println(BarBills.getID());
        YqgHands hand=new YqgHands();
        hand.setHandBrand(BarBills.getHandBrand());

        hand.setLockedState("0");
        List<YqgHands> hlist=yqgHandsService.selectYqgHandsList(hand);
        System.out.println(hlist.size());
        yqgConsumptionDetails.setBarBillsId(BarBills.getID());
        /*yqgConsumptionDetails.setHandState("1");*/
        yqgConsumptionDetails.setConsumptionStatus("0");
        //这里是留牌和自己记录
        List<YqgConsumptionDetails> ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        List<YqgConsumptionDetails> bplist=new ArrayList<>();
        //国旅并牌记录
        for(int i=0;i<ylist.size();i++){

            if(ylist.get(i).getMergeBarBillsId()!=null){
                bplist.add(ylist.get(i));
            }
        }
        ylist.removeAll(bplist);
        for(int i=0;i<ylist.size();i++){
            if(ylist.get(i).getBeforeBarBillsId()!=null){
                ylist.get(i).setNote10("来自"+ylist.get(i).getBeforeLeave()+"的留牌");
            }
        }
        //并牌查询
        yqgConsumptionDetails.setBarBillsId(null);
        yqgConsumptionDetails.setMergeBarBillsId(BarBills.getID());
        List<YqgConsumptionDetails> blist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        List<YqgConsumptionDetails> removelist=new ArrayList<>();
        for(int i=0;i< blist.size();i++){

            if(blist.get(i).getMergeBarBillsId().equals(BarBills.getID())&&blist.get(i).getBarBillsId().equals(BarBills.getID())){
                removelist.add(blist.get(i));
            }
        }
        if(removelist.size()!=0){
            blist.removeAll(removelist);
        }
        for(int i=0;i<blist.size();i++){
            if(blist.get(i).getMergeBarBillsId()!=null){
                blist.get(i).setNote10("来自"+blist.get(i).getHandBrand()+"的并牌");
            }
        }
        ylist.addAll(blist);
        return getDataTable(ylist);

        /*if(BarBills.getMergeStatus().equals("0")&&hlist.size()!=0){
            return  getDataTable(new ArrayList<YqgConsumptionDetails>());
        }
        else if(BarBills.getMergeStatus().equals("0")&&hlist.size()==0){
            yqgConsumptionDetails.setBarBillsId(BarBills.getID());
            *//*yqgConsumptionDetails.setHandState("1");*//*
            yqgConsumptionDetails.setConsumptionStatus("0");
            List<YqgConsumptionDetails> ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
            List<YqgConsumptionDetails> bplist=new ArrayList<>();
            for(int i=0;i<ylist.size();i++){

                if(ylist.get(i).getMergeBarBillsId()!=null){
                    bplist.add(ylist.get(i));
                }
            }
            ylist.removeAll(bplist);
            for(int i=0;i<ylist.size();i++){
                if(ylist.get(i).getBeforeBarBillsId()!=null){
                    ylist.get(i).setNote10("来自"+ylist.get(i).getBeforeLeave()+"的留牌");
                }
            }
            List<YqgConsumptionDetails> gylist=new ArrayList<>();
            for(int i=0;i<ylist.size();i++){
                if(ylist.get(i).getEntryName().equals("过夜费")){
                    Date retime= ylist.get(i).getRecordingTime();
                    LocalDateTime Record= LocalDateTime.ofInstant(retime.toInstant(), ZoneId.systemDefault());
                    String date=DateUtils.dateTimeNow("yyyy-MM-dd");
                    String start=date+" 00:00:00";
                    String end=date+" 06:00:00";
                    Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
                    Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
                    LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
                    LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());
                    if(Record.isAfter(startTime)&&Record.isBefore(endTime)){
                        LocalDateTime newDateTime = Record.plusHours(2);
                        LocalDateTime currentDateTime = LocalDateTime.now();
                        if(currentDateTime.isBefore(newDateTime)){
                            gylist.add(ylist.get(i));
                        }

                    }

                }
            }
            ylist.removeAll(gylist);
            return getDataTable(ylist);
        }
        else{
            yqgConsumptionDetails.setBarBillsId(BarBills.getID());
            *//*yqgConsumptionDetails.setHandState("1");*//*
            yqgConsumptionDetails.setConsumptionStatus("0");

            List<YqgConsumptionDetails> ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
            for(int i=0;i<ylist.size();i++){
                if(ylist.get(i).getBeforeBarBillsId()!=null){
                    ylist.get(i).setNote10("来自"+ylist.get(i).getBeforeLeave()+"的留牌");
                }
            }
            //并牌查询
            yqgConsumptionDetails.setBarBillsId(null);
            yqgConsumptionDetails.setMergeBarBillsId(BarBills.getID());
            List<YqgConsumptionDetails> blist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
            List<YqgConsumptionDetails> removelist=new ArrayList<>();
            for(int i=0;i< blist.size();i++){

                if(blist.get(i).getMergeBarBillsId().equals(BarBills.getID())&&blist.get(i).getBarBillsId().equals(BarBills.getID())){
                    removelist.add(blist.get(i));
                }
            }
            if(removelist.size()!=0){
                blist.removeAll(removelist);
            }
            for(int i=0;i<blist.size();i++){
                if(blist.get(i).getMergeBarBillsId()!=null){
                    blist.get(i).setNote10("来自"+blist.get(i).getHandBrand()+"的并牌");
                }
            }
            if(blist.size()==0){
                List<YqgConsumptionDetails> gylist=new ArrayList<>();
                for(int i=0;i<ylist.size();i++){
                    if(ylist.get(i).getEntryName().equals("过夜费")){
                        Date retime= ylist.get(i).getRecordingTime();
                        LocalDateTime Record= LocalDateTime.ofInstant(retime.toInstant(), ZoneId.systemDefault());
                        String date=DateUtils.dateTimeNow("yyyy-MM-dd");
                        String start=date+" 00:00:00";
                        String end=date+" 06:00:00";

                        Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
                        Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
                        LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
                        LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());
                        if(Record.isAfter(startTime)&&Record.isBefore(endTime)){
                            LocalDateTime newDateTime = Record.plusHours(2);
                            LocalDateTime currentDateTime = LocalDateTime.now();
                            if(currentDateTime.isBefore(newDateTime)){
                                gylist.add(ylist.get(i));
                            }

                        }

                    }
                }
                ylist.removeAll(gylist);
                ylist.addAll(blist);
                return getDataTable(ylist);
            }
            else{
                ylist.addAll(blist);
                List<YqgConsumptionDetails> gylist=new ArrayList<>();
                for(int i=0;i<ylist.size();i++){
                    if(ylist.get(i).getEntryName().equals("过夜费")){
                        Date retime= ylist.get(i).getRecordingTime();
                        LocalDateTime Record= LocalDateTime.ofInstant(retime.toInstant(), ZoneId.systemDefault());
                        String date=DateUtils.dateTimeNow("yyyy-MM-dd");
                        String start=date+" 00:00:00";
                        String end=date+" 06:00:00";
                        Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
                        Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
                        LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
                        LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());
                        if(Record.isAfter(startTime)&&Record.isBefore(endTime)){
                            LocalDateTime newDateTime = Record.plusHours(2);
                            LocalDateTime currentDateTime = LocalDateTime.now();
                            if(currentDateTime.isBefore(newDateTime)){
                                gylist.add(ylist.get(i));
                            }

                        }

                    }
                }
                ylist.removeAll(gylist);
                return getDataTable(ylist);
            }

        }*/
    }
    /**
     * 查询消费详细列表
     */
    /*@RequiresPermissions("project:whd:duizhang:list")*/
    @GetMapping("/checktimelist")
    public TableDataInfo checktimelist(YqgBarBills yqgBarBills)
    {
        startPage();
        System.out.println(yqgBarBills);
        List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsListbychecktime(yqgBarBills);
        return getDataTable(list);
    }

    /**
     * 导出消费详细列表
     */
   /* @RequiresPermissions("project:whd:duidan:export")*/
    @Log(title = "结账记录导出", businessType = BusinessType.EXPORT,logicalKey = "handBrand")
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails)
    {
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        ExcelUtil<YqgConsumptionDetails> util = new ExcelUtil<YqgConsumptionDetails>(YqgConsumptionDetails.class);
        util.exportExcel(response, list, "消费详细数据");
    }

    /**
     * 获取消费详细详细信息
     */
   /* @RequiresPermissions("project:whd:duidan:query")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        YqgHands hands=new YqgHands();
        hands.setHandBrand(id);
        YqgBarBills yqgBarBills=new YqgBarBills();
        yqgBarBills.setHandBrand(id);
        yqgBarBills.setHandState("1");
        /*hands.setHandState();*/
        List<YqgHands> wulist = yqgHandsService.selectYqgHandsList(hands);
        if(wulist.size()==0){
            return  new AjaxResult(500,id+"手牌不存在，请输入正确的手牌号");
        }else{
            List<YqgBarBills> list = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
            if(list.size()==0){
                return  new AjaxResult(500,id+"手牌不在台，请输入在台手牌");
            }else{
                return  new AjaxResult(200,"");
            }
        }

    }

    /**
     * 新增消费详细
     */
    /*@RequiresPermissions("project:whd:duidan:add")*/
    @Log(title = "消费详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 修改消费详细
     */
    /*@RequiresPermissions("project:whd:duidan:edit")*/
    @Log(title = "消费详细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        return toAjax(yqgConsumptionDetailsService.updateYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 删除消费详细
     */
   /* @RequiresPermissions("project:whd:duidan:remove")*/
    @Log(title = "消费详细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
    }
    /**
     * 计算合计
     */
    public int gettotal(YqgBarBills yqgBarBills)
    {

        YqgConsumptionDetails ConsumptionDetails=new YqgConsumptionDetails();
        System.out.println(yqgBarBills.getID());
        System.out.println(yqgBarBills);
        yqgBarBills.setHandState("0");
        YqgBarBills BarBills=yqgBarBillsService.selectYqgBarBillsByID(yqgBarBills.getID());
        List<YqgConsumptionDetails> ylist=new ArrayList<>();
        if(BarBills.getRetentionStatus().equals("1")){
            ConsumptionDetails.setBarBillsId(BarBills.getID());

            ConsumptionDetails.setRetentionStatus("1");

            ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);


        }else{
            ConsumptionDetails.setBeforeBarBillsId(BarBills.getID());


            //先查被留出去的记录
            List<YqgConsumptionDetails> llist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);

            ConsumptionDetails.setBeforeBarBillsId(null);
            ConsumptionDetails.setBarBillsId(BarBills.getID());
            //再查自己的记录
            ylist = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(ConsumptionDetails);
            //过滤留进来的记录
            List<YqgConsumptionDetails> removelist=new ArrayList<>();
            for(int i=0;i<ylist.size();i++){
                if(ylist.get(i).getBeforeBarBillsId()==null){

                }else{
                    if (!ylist.get(i).getBeforeBarBillsId().equals(BarBills.getID())){
                        removelist.add(ylist.get(i));
                    }
                }

            }
            ylist.removeAll(removelist);
            if(llist.size()!=0){
                ylist.addAll(llist);
            }


        }
        int total=0;
        for (int i=0;i<ylist.size();i++){
            if(ylist.get(i).getConsumptionStatus().equals("0")){
                if(ylist.get(i).getEntryName().equals("过夜费")){
                    Date retime= ylist.get(i).getRecordingTime();
                    LocalDateTime Record= LocalDateTime.ofInstant(retime.toInstant(), ZoneId.systemDefault());
                    String date= DateUtils.dateTimeNow("yyyy-MM-dd");
                    String start=date+" 00:00:00";
                    String end=date+" 06:00:00";
                    Date startdate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",start);
                    Date enddate= DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",end);
                    LocalDateTime startTime= LocalDateTime.ofInstant(startdate.toInstant(), ZoneId.systemDefault());
                    LocalDateTime endTime = LocalDateTime.ofInstant(enddate.toInstant(), ZoneId.systemDefault());
                    if(Record.isAfter(startTime)&&Record.isBefore(endTime)){
                        LocalDateTime newDateTime = Record.plusHours(2);
                        LocalDateTime currentDateTime = LocalDateTime.now();
                        if(currentDateTime.isBefore(newDateTime)){
                            total=total+Integer.valueOf(ylist.get(i).getTotalAmount());
                        }

                    }

                }
                else{
                    total=total+Integer.valueOf(ylist.get(i).getTotalAmount());
                }

            }

        }
        return total;
    }


}
